<?php
class WeiboService extends BaseDb2Service
{
	public function __construct($table='')
	{
		$this->dao	= new Base2DbDao($table ? $table : 'weibo');
	}
	
	public function getListByIds($ids)
	{
		if (empty($ids))
		{
			return array();
		}
		$result = $this->rows(array('id'=>array('in'=>$ids)));
		$list = array();
		if ($result)
		foreach($result as $item){
			$list[$item['id']] = $item;
			unset($list[$item['id']]['id']);
		}
		unset($result);
		return $list;
	}
	
	public function addData($data,$is_comment_parent=0)
	{
		$share_id = $this->add($data);
		if ($share_id)
		{
			$atme_list = array();
			$pattern = "/@([^\f\n\r\t\v@ ]{2,20}?)(?:\:| )/";
			preg_match_all($pattern,$data['content'],$atme_list);
			if(!empty($atme_list[1]))
			{
				$atme_list[1] = array_unique($atme_list[1]);
				$users = array();
				foreach($atme_list[1] as $user)
				{
					if(!empty($user))
					{
						$users[] = $user;
					}
				}
				$userService = new UserService();
				$users 	= $userService->rows(array('username'=>array('in'=>$users)),'',array('id'));
				if ($users)
				{
					
					$sqls = array();
					foreach($users as $item)
					{
						$sqls[] = '('.$item['id'].', '.$share_id.')';
					}
					$sql = 'INSERT IGNORE INTO '.$this->Table('weibo_atme').'(user_id, weibo_id) VALUES '.join(',', $sqls);
					$this->dao->execute($sql);
				}
			}
			
			$parent_map = array();
			if (isset($data['parent_id']) && $data['parent_id'])
			{
				$parent_map['repost_num'] = 'expression:repost_num+1';
			}
			
			if ($is_comment_parent)
			{
				$map = array(
					'user_id'=>$data['user_id'],
					'weibo_id'=>$data['parent_id'],
					'content'=>$data['content'],
				);
				$weiboCommentService	= new BaseDbService('weibo_comment');
				$weiboCommentService->add($map);
				
				$parent_map['comment_num'] = 'expression:comment_num+1';
				
			}
			
			if($parent_map)
			{
				$this->update($data['parent_id'], $parent_map);
			}
			
			$userService	= new BaseDbService('user');
			$userService->update($data['user_id'], array('weibo_num'=>'expression:weibo_num+1'));
			
			
			
		}
		
		return $share_id;
	}
	
	function getCommentsToUser($user_id){
		$user_id = (int)$user_id;
		$sql = 'SELECT COUNT(*) AS num FROM '.$this->Table('weibo_comment').' C JOIN '.$this->Table('weibo');
		$sql .= ' W ON C.weibo_id=W.id WHERE W.user_id='.$user_id;
		$result = $this->dao->select($sql);
		$num = $result[0]['num'];
		$rows = array();
		if ($num > 0)
		{
			$sql = 'SELECT C.*,W.content as re_content FROM '.$this->Table('weibo_comment').' C JOIN '.$this->Table('weibo');
			$sql .= ' W ON C.weibo_id=W.id WHERE W.user_id='.$user_id.' ORDER BY C.id DESC ';
			$rows = $this->dao->select($sql);
		}
		return array('count'=>$num,'rows'=>$rows);
		
	}
	
	function getListAtMe($user_id){
		$user_id = (int)$user_id;
		$sql = 'SELECT COUNT(*) AS num FROM '.$this->Table('weibo_atme').' C JOIN '.$this->Table('weibo');
		$sql .= ' W ON C.weibo_id=W.id WHERE C.user_id='.$user_id;
		$result = $this->dao->select($sql);
		$num = $result[0]['num'];
		$rows = array();
		if ($num > 0)
		{
			$sql = 'SELECT W.* FROM '.$this->Table('weibo_atme').' C JOIN '.$this->Table('weibo');
			$sql .= ' W ON C.weibo_id=W.id WHERE C.user_id='.$user_id.' ORDER BY W.id DESC ';
			$rows = $this->dao->select($sql);
		}
		return array('count'=>$num,'rows'=>$rows);
	}
	
}